﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WStest.Models;

namespace WStest.Controllers
{
    public class ChatController : Controller
    {
        Service service = new Service();
        //
        // GET: /Chat/

        public ActionResult Index()
        {
         //       service.GetMessageAfter(MessageID, aftertime
            //object chattime = Session["ChatTime"];
            //object chatid = Session["ChatId"];
            long date = service.GetUnixEpoch2(DateTime.Now.AddHours(-1));
            //int postId = 0;
            //if (chattime != null)
            //{
            //    date = long.Parse(chattime.ToString());
            //}
            //else
            //{
            //    date = DateTime.Now.Millisecond;
            //    Session["ChatTime"] = date;
            //}
            //if (chatid != null)
            //{
            //    postId = int.Parse(chatid.ToString());
            //}
            //else
            //{
            //    Session["ChatId"] = "0";
            //}
            List<ChatView> list = service.GetMessageAfter(0, date);
            int max = list.Count;
            int i = 10;
            if (i > max)
                i = max;
            return View(list.GetRange(max-i,i));
        }

        public ActionResult AddChatMessage(ChatView cv)
        {
            UserView u = service.GetUserByUsername(User.Identity.Name);
            service.AddChatMessage(service.GetUnixEpoch2(DateTime.Now), cv.content, u.Id);
            return RedirectToAction("Index");
        }

        //
        // GET: /Chat/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /Chat/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Chat/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /Chat/Edit/5
 
        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /Chat/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Chat/Delete/5
 
        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /Chat/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
